﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：王明亮
 * 创建时间：2023/9/19 9:40:02
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BaiduAIAuditClient.Helper
{
    /// <summary>
    /// ImageHelper 图片转换帮助类
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// 图片转为Byte数组
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static byte[] Image2Bytes(string imagePath)
        {
            try
            {
                byte[] photo;
                Image img = new Bitmap(imagePath);
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Png);
                photo = stream.ToArray();
                stream.Close();
                return photo;
              
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 图片转为Byte数组
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(string imagePath)
        {
            try
            {
                FileStream fs = new FileStream(imagePath, FileMode.Open);
                byte[] byteData = new byte[fs.Length];
                fs.Read(byteData, 0, byteData.Length);
                fs.Close();
                return byteData;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Byte数组转为Image
        /// </summary>
        /// <param name="photo"></param>
        /// <returns></returns>
        public static Image bytes2Image(byte[] photo)
        {
            try
            {
                MemoryStream ms = new MemoryStream(photo, true);
                Image img = Image.FromStream(ms);
                return img;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
